package com.nashuju.web.service.sw;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import org.durcframework.core.expression.ExpressionQuery;
import org.durcframework.core.expression.subexpression.ValueExpression;
import org.durcframework.core.service.CrudService;
import com.nashuju.web.common.Config;
import com.nashuju.web.dao.sw.STaskDao;
import com.nashuju.web.entity.sw.STask;
import com.nashuju.web.entity.sw.STaskSchedule;
import com.nashuju.web.entity.sw.STaskScheduleSch;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class STaskService extends CrudService<STask, STaskDao> {
	
	@Autowired
	STaskScheduleService sTaskScheduleService;
	//按照循环规则生成发布时间
	
	public boolean createSTaskSchedule(STask sTask){
		Date releaseTime = sTask.getReleaseTime();
		Date finishTime = sTask.getPlanFinishTime();
		Date arriveTime = sTask.getPlanArriveTime();
		// 循环发布
		if (sTask.getLoopTime() > 0 && sTask.getLoopPeriod() > 0 && sTask.getReleaseTime() != null) 
		{
			try {
				for (int i = 0; i < sTask.getLoopTime(); i++) {
					STaskSchedule sTaskSchedule = new STaskSchedule();
					releaseTime.setTime(releaseTime.getTime()+i*(24*60*60*1000));
					finishTime.setTime(finishTime.getTime()+i*(24*60*60*1000));
					arriveTime.setTime(arriveTime.getTime()+i*(24*60*60*1000));
					sTaskSchedule.setTaskId(sTask.getId());
					sTaskSchedule.setStatus(Config.TOBESEND);
					sTaskSchedule.setUserId(sTask.getEngineer());
					sTaskSchedule.setSendTime(releaseTime);
					sTaskSchedule.setPlanArriveTime(arriveTime);
					sTaskSchedule.setPlanFinishTime(finishTime);
					sTaskScheduleService.save(sTaskSchedule);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			return true;
		}
		// 定时发布
		else if(sTask.getReleaseTime() != null ) 
		{
			try {
				STaskSchedule sTaskSchedule = new STaskSchedule();
				sTaskSchedule.setSendTime(releaseTime);
				sTaskScheduleService.save(sTaskSchedule);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return true;
		}
		else 
		{
			return false;
		}
		
	}
	
	public boolean delSTaskSchedule(STask sTask) {
		if (sTask.getId()!=0) 
		{
			try {
				ExpressionQuery query = new ExpressionQuery();
				query.add(new ValueExpression("task_id", sTask.getId()));
				query.setQueryAll(true);
				List<STaskSchedule> cList = sTaskScheduleService.find(query);
				if (cList.size() != 0) {
					for (STaskSchedule sTaskSchedule : cList) {
						sTaskScheduleService.del(sTaskSchedule);
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			return true;
		}
		else {
			return  false;
		}
	}
	
	
	
	public  List <STaskSchedule>  getReleaseList()
	{
		Date now = new Date();
		STaskScheduleSch sTaskScheduleSch = new STaskScheduleSch();
		sTaskScheduleSch.setReleaseStartSch(now);
		sTaskScheduleSch.setReleaseEndSch(now);
		List<STaskSchedule> listSTaskSchedule = null;
		try {
			listSTaskSchedule = sTaskScheduleService.find(ExpressionQuery.buildQueryAll().addAnnotionExpression(sTaskScheduleSch));
		    for (STaskSchedule sTaskSchedule : listSTaskSchedule) {
		    	sTaskSchedule.setStatus(Config.RELEASED);
		    	sTaskScheduleService.update(sTaskSchedule);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return listSTaskSchedule;
	}
	
	public static int count = 0;
    public void showTimer() 
    {
    	
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
            	getReleaseList();
                count = 1 ;
                System.out.println("时间=" + new Date() + " 执行了" + count + "次"); // 1次
            }
        };

        //设置执行时间
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);//每天
        //定制每天的21:09:00执行，
        calendar.set(year, month, day, 00, 10, 00);
        Date date = calendar.getTime();
        Timer timer = new Timer();
        System.out.println(date);
        
        int period = 2 * 1000;
        //每天的date时刻执行task，每隔2秒重复执行
        timer.schedule(task, date);
        //每天的date时刻执行task, 仅执行一次
        //timer.schedule(task, date);
    }
	
	
	
	

}